package com.redteam.xomtro.fragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.redteam.xomtro.R;
import com.redteam.xomtro.WellcomeActivity;
import com.redteam.xomtro.adapters.SubImageDetailsAdapter;
import com.redteam.xomtro.classes.StackBlurManager;
import com.redteam.xomtro.classes.Support;
import com.redteam.xomtro.entities.Room;
import com.redteam.xomtro.views.ObservableScrollView;

import java.io.ByteArrayOutputStream;

public class SubImageDetailsParallaxFragment extends Fragment {

    private SubImageDetailsAdapter mSubImageDetailsAdapter;
    private ObservableScrollView roomScrollView;
    private ImageView blurImg;
    private int blurImgWidth;
    private int blurImgHeight;
    private StackBlurManager _stackBlurManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        super.onCreateView(inflater, container, savedInstanceState);
        View v = inflater.inflate(R.layout.fragment_subimage_details, container, false);
        int imageResource =0;
        int index=0;
//        String idRoom =null;
//        Room r =Room.getListRoomExample().get(0);
        imageResource = getArguments().getInt("image",-1);
//        imageResource = r.getDemo_subImage().get(index);
        final ImageView image = (ImageView) v.findViewById(R.id.image);
        if(imageResource!=-1) {
            image.setImageResource(imageResource);
        }
        blurImg = (ImageView) v.findViewById(R.id.ivBlurImg);
        blurImg.setAlpha(0f);

        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        Bitmap tmp = null;
        if(imageResource!=-1) {
            tmp = BitmapFactory.decodeResource(getResources(), imageResource);
        }else{
            Toast.makeText(getActivity(),"img null",Toast.LENGTH_SHORT);
        }
        blurImgWidth = WellcomeActivity.widthScreen;
        blurImgHeight = WellcomeActivity.heightScreen;
        if (tmp!=null){
            tmp.compress(Bitmap.CompressFormat.JPEG, 5, bao);
        }
        byte[] bitmapdata = bao.toByteArray();
        _stackBlurManager = new StackBlurManager(BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length));

        if (Support.isAPIHigherHoneycomb())
            new BlurImageAsyncTask().execute();
        image.post(new Runnable() {
            @Override
            public void run() {
                Matrix matrix = new Matrix();
                matrix.reset();

                float wv = image.getWidth();
                float hv = image.getHeight();
                float wi = image.getDrawable().getIntrinsicWidth();
                float hi = image.getDrawable().getIntrinsicHeight();

                float width = wv;
                float height = hv;

                if (wi / wv > hi / hv) {
                    matrix.setScale(hv / hi, hv / hi);
                    width = wi * hv / hi;
                } else {
                    matrix.setScale(wv / wi, wv / wi);
                    height = hi * wv / wi;
                }

                matrix.preTranslate((wv - width) / 2, (hv - height) / 2);
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                image.setImageMatrix(matrix);
            }
        });
        roomScrollView = (ObservableScrollView) getActivity().findViewById(R.id.svRoomItem);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (roomScrollView.getVisibility() == View.VISIBLE) {
                    roomScrollView.setVisibility(View.GONE);
                } else {
                    roomScrollView.setVisibility(View.VISIBLE);
                }
            }
        });
        return v;
    }

    public void setAdapter(SubImageDetailsAdapter subImageDetailsAdapter) {
        mSubImageDetailsAdapter = subImageDetailsAdapter;
    }

    class BlurImageAsyncTask extends AsyncTask<Void, Bitmap, Bitmap> {
        @Override
        protected Bitmap doInBackground(Void... iv) {
            try {
                return _stackBlurManager.process(100);
            } catch (Exception ex) {
                return null;
            }
        }
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            blurImg.setImageBitmap(Bitmap.createScaledBitmap(bitmap, blurImgWidth, blurImgHeight, false));
        }
    }


}
